using System;
using Unity.VisualScripting;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private static PlayerController _instance;
    public static PlayerController Instance
    {
        get
        {
            if(_instance == null)
            {
                _instance = FindAnyObjectByType<PlayerController>();
            }
            return _instance;
        }
    }

    [SerializeField] float moveSpeed;
    [SerializeField] Transform originParent;
    [SerializeField] private SpriteRenderer bodySr;
    public CameraShake cameraShake;
    float h, v;
    bool isInDialogue;
    bool canMove = true;
    bool isOnBoat;

    void Update()
    {
        Move();
    }

    void Move()
    {
        if(CannotMoveCase()) { return; }
        h = Input.GetAxisRaw("Horizontal");
        v = Input.GetAxisRaw("Vertical");
        Vector3 direction = Vector3.Normalize(new Vector3(h, v));
        transform.position += direction * moveSpeed * Time.deltaTime;
    }
    bool CannotMoveCase()
    {
        return isInDialogue
            || !canMove
            || isOnBoat;
    }

    public void ResetState()
    {
        GetCamera().orthographicSize = Constants.OrthographicSize;
        transform.rotation = Quaternion.Euler(0, 0, 0);
        bodySr.enabled = true;
    }
    public bool GetIsInDialogue()
    {
        return isInDialogue;
    }
    public Transform GetOriginParent()
    {
        return originParent;
    }
    public void SetIsInDialogue(bool value)
    {
        isInDialogue = value;
    }
    public void SetCanMove(bool value)
    {
        canMove = value;
    }
    public void SetIsOnBoat(bool value)
    {
        isOnBoat = value;
    }
    public Camera GetCamera()
    {
        return cameraShake.GetComponent<Camera>();
    }
    public float GetDistanceSqr(Vector3 pos)
    {
        return Vector3.SqrMagnitude(transform.position - pos);
    }

    public void HidePlayer()
    {
        SetCanMove(false);
        bodySr.enabled = false;
    }
}
